package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.MenuTable;
import com.wanczy.service.MenuOpService;
import com.wanczy.service.impl.MenuOpServiceImpl;
import com.wanczy.util.GetIDK;
import com.wanczy.util.GetLevel;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class MenuInsServlet
 */
@WebServlet("/servlet/MenuInsServlet")
public class MenuInsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int lev = GetLevel.getLevel();
		String le = "";
		if(lev<10) {
			le = "00"+lev;
		}else if(lev<100) {
			le = "0"+lev;
		}else {
			le = ""+lev;
		}
		String menuUrl = "";
		int menuId = GetIDK.getIdk();
		String menuName = request.getParameter("menuName");
		String menuDescribe = request.getParameter("menuDescribe");
		int menuState = Integer.parseInt(request.getParameter("menuState"));
		menuUrl = request.getParameter("menuUrl");
		String level = request.getParameter("menuLevel");
		MenuOpService service = new MenuOpServiceImpl();
		String menuLevel = service.findByMenuName(level)+le;
		MenuTable menuTable = new MenuTable(menuId,menuName,menuDescribe,menuState,menuUrl,menuLevel);
		boolean flag = service.doIns(menuTable);
		JSONArray jsonA = new JSONArray();
		JSONObject jsonO = new JSONObject();
		jsonO.put("result", flag);
		jsonA.add(jsonO);
		out.print(jsonA.toString());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
